package cn.iocoder.yudao.im.task;

import cn.iocoder.yudao.im.domain.NoticeImMessage;
import cn.iocoder.yudao.im.enums.MessageStatus;
import cn.iocoder.yudao.im.service.NoticeImMessageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Component
@Slf4j
public class ImTask {

    @Resource
    NoticeImMessageService fancyOfficeImMessageService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void cron() {
        LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(30);
        NoticeImMessage noticeImMessage = new NoticeImMessage();
        noticeImMessage.setStatus(MessageStatus.DELETE.code());
        boolean update = fancyOfficeImMessageService.update(noticeImMessage, new LambdaQueryWrapper<NoticeImMessage>().lt(NoticeImMessage::getSendTime, sevenDaysAgo));
        log.info("<<<<<开始清除30天前的聊天记录>>>>> 是否成功：{} >>>>>每天一点执行一次！", update);
    }

}
